package swu.cx.master_of_mental_arithmetic

import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.navigation.NavController
import androidx.navigation.Navigation
import androidx.navigation.ui.NavigationUI

class MainActivity : AppCompatActivity() {
    lateinit var navController:NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        navController = Navigation.findNavController(this,R.id.fragment)
        NavigationUI.setupActionBarWithNavController(this,navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        if (navController.currentDestination?.id ==R.id.questionFragment){
            AlertDialog.Builder(this)
                .setTitle(getString(R.string.dialog_show_text))
                .setPositiveButton(getString(R.string.positiveText)){ dialogInterface: DialogInterface, i: Int ->
                    navController.navigateUp()
                }
                .setNegativeButton(getString(R.string.negativeText)){ dialogInterface: DialogInterface, i: Int ->

                }
                .create()
                .show()
        }else if(navController.currentDestination?.id ==R.id.titleFragment){
            finish()
        } else{
            navController.navigate(R.id.titleFragment)
        }
        return super.onNavigateUp()
    }

    override fun onBackPressed() {
        onSupportNavigateUp()
    }
}